﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy;
using System.Xml.Linq;

namespace SPMetadataManager.Taxonomy
{
    public class Exporter
    {
        public void ExportTermStoreCollection(string siteUrl, out XElement termStoreCollectionNode, bool recurse = true)
        {
            termStoreCollectionNode = new XElement("TermStoreCollection");

            try
            {
                
                termStoreCollectionNode.SetAttributeValue("SiteCollection", siteUrl);

                if (recurse)
                {
                    using (SPSite site = new SPSite(siteUrl))
                    {
                        TaxonomySession session = new TaxonomySession(site);

                        foreach (TermStore termStore in session.TermStores)
                        {
                            XElement termStoreElement;
                            ExportTermStore(termStore, out termStoreElement);
                            termStoreCollectionNode.Add(termStoreElement);
                        }
                    }
                }
            }
            catch (Exception e)
            {
            }


        }

        public void ExportTermStore(TermStore termStore, out XElement termStoreElement, bool recurse = true)
        {
            termStoreElement = new XElement("TermStore");

            try
            {
                termStoreElement.SetAttributeValue("Name", termStore.Name);
                if (recurse)
                {
                    foreach (Group group in termStore.Groups)
                    {
                        XElement groupElement;
                        ExportGroup(group, out groupElement);
                        termStoreElement.Add(groupElement);
                    }
                }
            }
            catch (Exception e)
            {

            }
        }

        public void ExportGroup(Group group, out XElement groupElement, bool recurse = true)
        {
            groupElement = new XElement("Group");

            try
            {
                groupElement.SetAttributeValue("Name", group.Name);
                if (recurse)
                {
                    foreach (TermSet termSet in group.TermSets)
                    {
                        XElement termSetElement;
                        ExportTermSet(termSet, out termSetElement);
                        groupElement.Add(termSetElement);
                    }
                }

            }
            catch (Exception e)
            {
            }

           
        }

        public void ExportTermSet(TermSet termSet, out XElement termSetElement, bool recurse = true)
        {
            termSetElement = new XElement("TermSet");

            try
            {
                termSetElement.SetAttributeValue("Name", termSet.Name);

                if (recurse)
                {

                    foreach (Term term in termSet.Terms)
                    {
                        XElement termElement;
                        ExportTerm(term, out termElement);
                        termSetElement.Add(termElement);
                    }
                }
            
            }
            catch (Exception e)
            {
            }

        }

        public void ExportTerm(Term term, out XElement termElement, bool recurse = true)
        {
            termElement = new XElement("Term");

            try
            {
                termElement.SetAttributeValue("Name", term.Name);
                
                if (recurse)
                {
                    foreach (Term childTerm in term.Terms)
                    {
                        XElement childTermElement;
                        ExportTerm(childTerm, out childTermElement);
                        termElement.Add(childTermElement);
                    }
                }
            }
            catch
            {
            }
        }
    }
}
